package com.simon.zhujie;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by changf on 2017/7/13.
 */
@Component
public class JiexiAnnotation implements ApplicationContextAware {

    private static ApplicationContext context;

    public void resolve() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(RpcService.class);
        for (Object obj : beansWithAnnotation.values()){
            String value = obj.getClass().getAnnotation(RpcService.class).value();
            System.out.println("get annotation value="+value);
            Method say = obj.getClass().getMethod("say", new Class[]{String.class});
            Object simon = say.invoke(obj, "simon");
            System.out.println(simon);

        } ;

    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }


    public static void main(String[] args)throws Exception {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring1.xml");
        JiexiAnnotation s=new JiexiAnnotation();
        s.resolve();
    }
}
